<template>
  <div class="update-password">
    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <span>修改密码</span>
        </div>
      </template>
      
      <el-form
        ref="formRef"
        :model="form"
        :rules="rules"
        label-width="100px"
        class="password-form"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username" placeholder="请输入用户名" />
        </el-form-item>
        
        <el-form-item label="新密码" prop="password">
          <el-input
            v-model="form.password"
            type="password"
            placeholder="请输入新密码"
            show-password
          />
        </el-form-item>
        
        <el-form-item label="确认密码" prop="surePassword">
          <el-input
            v-model="form.surePassword"
            type="password"
            placeholder="请再次输入新密码"
            show-password
          />
        </el-form-item>
        
        <el-form-item>
          <el-button type="primary" @click="submitForm">确认修改</el-button>
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import axios from 'axios'

const formRef = ref(null)

const form = reactive({
  username: '',
  password: '',
  surePassword: ''
})

const validatePass = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请输入密码'))
  } else {
    if (value.length < 6 || value.length > 20) {
      callback(new Error('密码长度必须在6-20位之间'))
    } else if (!/^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{6,20}$/.test(value)) {
      callback(new Error('密码必须包含数字和字母'))
    } else if (form.surePassword !== '') {
      formRef.value?.validateField('surePassword')
    }
    callback()
  }
}

const validatePass2 = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请再次输入密码'))
  } else if (value !== form.password) {
    callback(new Error('两次输入密码不一致!'))
  } else {
    callback()
  }
}

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, validator: validatePass, trigger: 'blur' }
  ],
  surePassword: [
    { required: true, validator: validatePass2, trigger: 'blur' }
  ]
}

const submitForm = async () => {
  if (!formRef.value) return
  
  await formRef.value.validate(async (valid, fields) => {
    if (valid) {
      try {
        const response = await axios.post('/api/user/updatePassword', {
          username: form.username,
          password: form.password,
          surePassword: form.surePassword
        })
        
        if (response.data.code === 200) {
          ElMessage({
            message: '密码修改成功',
            type: 'success'
          })
          resetForm()
        } else {
          ElMessage({
            message: response.data.message || '密码修改失败',
            type: 'error'
          })
        }
      } catch (error) {
        if (error.response?.status === 429) {
          ElMessage({
            message: '操作太频繁，请稍后再试',
            type: 'warning'
          })
        } else {
          ElMessage({
            message: error.response?.data?.message || '系统错误',
            type: 'error'
          })
        }
      }
    } else {
      console.log('error submit!', fields)
    }
  })
}

const resetForm = () => {
  if (!formRef.value) return
  formRef.value.resetFields()
}
</script>

<style scoped>
.update-password {
  display: flex;
  justify-content: center;
  padding: 20px;
}

.box-card {
  width: 500px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.password-form {
  margin-top: 20px;
}
</style> 